package in.muton.edushare.pages.cp;

import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.cp.backup.BackupBase;
import in.muton.edushare.pages.cp.config.ConfigCP;
import in.muton.edushare.pages.cp.courseFBCP.CourseFeedbackPage;
import in.muton.edushare.pages.cp.semCP.SemesterCP;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.vo.Universities;

public class CPBase extends EduShareBasePage {

	protected IModel<Universities> univModel;
	public IModel<Universities> getUnivModel() {
		return univModel;
	}

	// private String[] mainMenu = {"Semester", "System Backup"};
	private static ArrayList<String> mainMenu = new ArrayList<String>();
	static {
		mainMenu.add("Semester");
		mainMenu.add("System Backup");
		mainMenu.add("Grading System");
		mainMenu.add("Configuration");
		mainMenu.add("Semester Grades");
		mainMenu.add("Course Feedback");
	}
	private String activeMenu;

	public CPBase(PageParameters params) {
		String p = params.getString("0", null);
		// Sanity Checks
		if (p == null)
			pageNotFound();
		Universities u = new UniversitiesDAO(getSessionORMInterface())
				.getUnivById(p);
		if (!new UnivRoleCheck(u, getLoggedInUser()).isAdmin())
			unauthorized();
		this.univModel = new CayenneModel<Universities>(u);
		init(0);
	}

	public CPBase(IModel<Universities> univModel, int leftMenuIndex) {
		this.univModel = univModel;
		init(leftMenuIndex);
	}

	protected void init(int leftMenuIndex) {
		activeMenu = mainMenu.get(leftMenuIndex);
		loadMainMenu();
	}

	private void loadMainMenu() {
		add(new ListView<String>("mcont", mainMenu) {
			private static final long serialVersionUID = -276307777733603160L;
			@Override
			protected void populateItem(final ListItem<String> item) {
				Link<Void> lnk;
				item.add(lnk = new Link<Void>("lnk") {
					private static final long serialVersionUID = 457393083743207932L;

					@Override
					public void onClick() {
						switch (item.getIndex()) {
						case 0:
							setResponsePage(new SemesterCP(univModel));
							break;

						case 1:
							setResponsePage(new BackupBase(univModel));
							break;

						case 2:
							setResponsePage(new UnivGradingPage(univModel));
							break;

						case 3:
							setResponsePage(new ConfigCP(univModel));
							break;
							
						case 4:
							setResponsePage(new PrevCourseGradesXLS(univModel));
							break;
						
						case 5:
							setResponsePage(new CourseFeedbackPage(univModel));
							break;
							
						default:
							break;
						}

						activeMenu = item.getModelObject();
					}

					@Override
					public boolean isVisible() {
						String s1 = item.getModelObject();
						String s2 = activeMenu;
						return (!s1.equals(s2));
					}

				});

				lnk.add(new Label("llnk", item.getModelObject()));

				Label lbl;
				item.add(lbl = new Label("lbl", item.getModelObject()) {

					private static final long serialVersionUID = -5610979894756186284L;

					@Override
					public boolean isVisible() {
						String s1 = item.getModelObject();
						String s2 = activeMenu;
						return (s1.equals(s2));
					}
				});

				if (lbl.isVisible())
					item.add(new AttributeAppender("class", true,
							new Model<String>("cp-menuSel"), " "));
			}

		});

	}

	@Override
	public String getPageTitle() {
		return "Control Panel";
	}

	@Override
	public String getUserRole() {
		return " : "
				+ new UnivRoleCheck(univModel.getObject(), getLoggedInUser())
						.getRoleAsString();
	}

}
